package com.example.hello1;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Mycurrency_thred implements Runnable{
    private static final String Tag="单独子线程类接口";
     Handler handler;

//    public Mycurrency_thred(Handler handler1){
//        this.handler=handler1;
//    }

    public void setHadeler(Handler handler) {

        this.handler = handler;
    }

    @Override
    public void run() {


        List<String> list1 = new ArrayList<String>();
//        try {
//            Thread.sleep(100);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        Log.i(Tag, "多线程程序开始");


            URL url = null;
            try {

//                Document doc2 = Jsoup.connect("https://www.usd-cny.com/bankofchina.htm").get();
                Document doc2 = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
                Log.i(Tag, "run:" + doc2.title());
                Elements tables = doc2.getElementsByTag("table");
                Element table1 = tables.get(1);
                Elements hrefs = table1.getElementsByTag("tr");

//                Log.i(Tag,"what+"+hrefs);
//                for(Element a:hrefs){
//                    Log.i(Tag,"what+"+a.text());
//                    list1.add(a.text());
//                }
                for (int i = 1; i < hrefs.size(); i++) {
                    Elements td = hrefs.get(i).getElementsByTag("td");
                    String cname = td.get(0).text();
                    String rate = td.get(5).text();
                    list1.add(cname + "——" + rate);

                }
                Log.i(Tag, "what+" + list1);


//                    if ("美元".equals(cname)) {
//                        bdl.putFloat("r1", 100f / Float.parseFloat(rate));
//                    } else if ("欧元".equals(cname)) {
//                        bdl.putFloat("r2", 100f / Float.parseFloat(rate));
//
//                    } else if ("韩元".equals(cname)) {
//                        bdl.putFloat("r3", 100f / Float.parseFloat(rate));
//
//                    }

//


            } catch (IOException e) {
                e.printStackTrace();
            }
            //发送消息给主线程
            Log.i(Tag, "传递消息2");
            Message msg = handler.obtainMessage(6, list1);
//            msg.what = 6;
//            msg.obj = list1;
            Log.i(Tag, "传递消息");
            handler.sendMessage(msg);
            Log.i(Tag, "睡眠1s之后传递消息");

        }

//        else{
//
//            Message msg = handler.obtainMessage();
//            msg.what = 5;
//            msg.obj = "不从网络获取数据";
//            handler.sendMessage(msg);
//            Log.i(Tag, "睡眠1s之后传递消息");
//        }
//    }

}
